package com.dev.mainual.throwable.trx;

import com.dev.mainual.throwable.base.CusException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ClassName: TrxService
 * Description:
 * Date: 2019/1/28 9:37 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TrxService {

    @Autowired
    private TrxDao trxDao;

    public void insert() {
        int insert = trxDao.insert();

        // try-catch 捕获异常后，不会回滚
        // 事务方法中，慎用 try-catch
        try {
            System.out.println(1 / 0);
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
        }
    }


    public void insert2() throws CusException {
        int insert = trxDao.insert();

        // 有时候业务需要，不得不try-catch，可以使用这样的方式处理
        try {
            System.out.println(1 / 0);
        } catch (ArithmeticException e) {
            throw new CusException(e.getMessage());
        }
    }

}
